package com.fmi.chesstern;

import java.util.Map;

import com.fmi.chesstern.ChessGame.GameStatus;
import com.fmi.chesstern.ChessGame.MoveStatus;
import com.fmi.chesstern.gameboard.GameField;
import com.fmi.chesstern.pieces.ChessPiece;
import com.fmi.chesstern.pieces.ChessPiece.PieceColor;
import com.fmi.chesstern.pieces.ChessPiece.PieceType;


public class CommandLineChess {

	private ChessGame game = null;
	
	public CommandLineChess() {
		game = ChessGame.getInstance();
	}
	
	public void undo() {
		game.undo();
		System.out.println("\n\nUndo move\n\n");
		print();
	}
	
	public void move(GameField start, GameField goal) {
		MoveStatus status = game.move(start, goal);
		switch (status) {
		case OK: {
			System.out.println("\n\n");
			System.out.println("Successfully moved from " + start + " to " + goal);
			print();
			break;
		}
		case ILLEGAL: System.out.println("Illegal move from " + start + " to " + goal + ""); break;
		case OPPONENT_TURN: System.out.println("Cannot move - it is not this player's turn"); break;
		case KING_EXPOSED: System.out.println("Cannot move - the king will be exposed"); break;
		case NO_PIECE: System.out.println("There is no piece at position " + start); break;
		}
		printGameStatus();
	}
	
	public void printGameStatus() {
		GameStatus status = game.getGameStatus();
		switch (status) {
		case NORMAL: break;
		case WHITE_CHECK: System.out.println("The white king is in check"); break;
		case BLACK_CHECK: System.out.println("The black king is in check"); break;
		case WHITE_CHECKMATE: System.out.println("The black wins"); break;
		case BLACK_CHECKMATE: System.out.println("The white wins"); break;
		case STALEMATE: System.out.println("Stalemate - it's a draw"); break;
		}
	}
	
	public void print() {
		GameField[][] fields = game.getBoard().getFields();
		Map<String, ChessPiece> pieces = game.getBoard().getPieces();
		ChessPiece currentPiece = null;
		String printString = "";
		
		System.out.println("   a b c d e f g h ");
		System.out.println("---------------------");
		
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 10; j++) {
				if (j == 0) {
					printString = i + "| ";
				} else if (j == 9) {
					printString = "|" + i;
				} else {
					currentPiece = pieces.get(fields[i][j - 1].toString());
					if (currentPiece == null) {
						printString = "0 ";
					} else {
						PieceType type = currentPiece.getType();
						switch (type) {
						case PAWN: printString = "p "; break;
						case ROOK: printString = "r "; break;
						case KING: printString = "k "; break;
						case QUEEN: printString = "q "; break;
						case BISHOP: printString = "b "; break;
						case KNIGHT: printString = "h "; break;
						}
						if (currentPiece.getColor() == PieceColor.BLACK) {
							printString = printString.toUpperCase();
						}
					}
				}
				System.out.print(printString);
			}
			System.out.println();
		}
		System.out.println("---------------------");
		System.out.println("   a b c d e f g h ");
	}
	
	public void executeCommand(String command) {
		game.executeCommand(command);
	}
	
	public static void main(String[] args) {
		CommandLineChess chess = new CommandLineChess();
		chess.print();
		chess.executeCommand("move a6 a5");
		chess.print();
		chess.executeCommand("undo");
		chess.print();
		
	}
}
